package com.project.web.system.controller.app;

import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.id.IDUtil;
import com.project.web.system.controller.common.BaseController;
import com.project.web.system.model.app.Role;
import com.project.web.system.model.app.RoleMenu;
import com.project.web.system.service.app.RoleMenuService;
import com.project.web.system.service.app.RoleService;
import com.project.web.util.exception.SYSTEM_RESP_CODE_ENUM;
import com.project.web.util.exception.SystemCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName RoleController
 * @Description
 * @Author WYY
 * @Date 2020/8/14 10:48 AM
 * @Version 1.0
 */
@Api(tags = "【Web】角色控制器")
@RestController("roleController")
@RequestMapping("/system/auth/role")
public class RoleController extends BaseController {


    Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "查询角色")
    @GetMapping(value = "/getRole")
    public Result getRole(Role role) {
        List<Role> roles = roleService.selectList(role);
        return getResult(roles);
    }

    @ApiOperation(value = "保存角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        if (role == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        role.setCreateDate(new Date());
        role.setId(IDUtil.getId());
        roleService.save(role);
        return getResult("保存成功");
    }


    @ApiOperation(value = "修改角色")
    @PostMapping("/edit")
    public Result edit(@RequestBody Role role) {
        if (role == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        roleService.update(role);
        return getResult("修改成功");
    }

    @ApiOperation(value = "删除角色")
    @GetMapping("/delete")
    public Result delete(Long id) {
        if (id == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        roleService.delete(id);
        return getResult("修改成功");
    }


    @ApiOperation(value = "角色赋予权限")
    @PostMapping("/givenMenu")
    public Result givenMenu(@RequestBody List<RoleMenu> roleMenus) {
        if (CollectionUtils.isEmpty(roleMenus)) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        RoleMenu roleMenu = roleMenus.get(0);
        roleMenuService.deleteByRoelId(roleMenu.getRoleId());
        for (RoleMenu rm : roleMenus) {
            rm.setId(IDUtil.getId());
            rm.setCreateDate(new Date());
        }
        roleMenuService.batchSave(roleMenus);
        return getResult("添加成功");
    }

    @ApiOperation(value = "查询角色权限")
    @GetMapping(value = "/getMenuByRoleId")
    public Result getMenuByRoleId(Long roleId) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(roleId);
        List<RoleMenu> roleMenus = roleMenuService.selectList(roleMenu);
        return getResult(roleMenus);
    }

}
